package com.qeeyou.apps.android.craftsman.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.activity.login.LoginActivity;
import com.qeeyou.apps.android.craftsman.activity.me.AboutActivity;
import com.qeeyou.apps.android.craftsman.activity.me.FeedbackActivity;
import com.qeeyou.apps.android.craftsman.activity.me.HelpActivity;
import com.qeeyou.apps.android.craftsman.activity.me.PersonInfoActivity;
import com.qeeyou.apps.android.craftsman.activity.me.MyReleaseActivity;
import com.qeeyou.apps.android.craftsman.activity.me.SafeActivity;
import com.qeeyou.apps.android.craftsman.activity.me.SettingActivity;
import com.qeeyou.apps.android.craftsman.activity.me.ShareActivity;
import com.qeeyou.apps.android.craftsman.activity.me.WalletActivity;
import com.qeeyou.apps.android.craftsman.event.LogoutEvent;
import com.qeeyou.apps.android.craftsman.po.LoginInfo;
import com.qeeyou.apps.android.craftsman.service.SystemContext;
import com.qeeyou.apps.android.craftsman.utils.ImageLoader;
import com.qeeyou.apps.android.craftsman.view.ImageTextButton;
import com.qeeyou.apps.android.craftsman.view.MeLongButton;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by xuchengxiong on 2017/12/14.
 */

public class MeFragment extends BaseFragment {

    private ImageView imgvAvatar;
    private ImageView imgvGender;
    private TextView txtvName;

    private ImageTextButton btnWallet;
    private ImageTextButton btnInfo4Me;
    private ImageTextButton btnSafe;
    private ImageTextButton btnShare;

    private MeLongButton btnHelp;
    private MeLongButton btnFeedback;
    private MeLongButton btnAbout;
    private MeLongButton btnSetting;

    public MeFragment() {
        super(R.layout.fragment_me);
    }

    @Override
    protected void doOnCreate(Bundle savedInstanceState) {

        EventBus.getDefault().register(this);

        setView();
        setEvent();

    }

    private void setView() {
        imgvAvatar = (ImageView) this.findViewById(R.id.imgvAvatar4Me);
        imgvGender = (ImageView) this.findViewById(R.id.imgvGender4Me);
        txtvName = (TextView) this.findViewById(R.id.txtvName4Me);

        btnWallet = (ImageTextButton) this.findViewById(R.id.btnWallet4Me);
        btnInfo4Me = (ImageTextButton) this.findViewById(R.id.btnInfo4Me);
        btnSafe = (ImageTextButton) this.findViewById(R.id.btnSafe4Me);
        btnShare = (ImageTextButton) this.findViewById(R.id.btnShare4Me);

        btnHelp = (MeLongButton) this.findViewById(R.id.btnHelp4Me);
        btnFeedback = (MeLongButton) this.findViewById(R.id.btnFeedback4Me);
        btnAbout = (MeLongButton) this.findViewById(R.id.btnAbout4Me);
        btnSetting = (MeLongButton) this.findViewById(R.id.btnSetting4Me);

    }

    private void setEvent() {
        imgvAvatar.setOnClickListener(onAvatarClickListener);
        btnWallet.setOnClickListener(onButtonClickListener);
        btnInfo4Me.setOnClickListener(onButtonClickListener);
        btnSafe.setOnClickListener(onButtonClickListener);
        btnShare.setOnClickListener(onButtonClickListener);

        btnHelp.setOnClickListener(onItemClickListener);
        btnFeedback.setOnClickListener(onItemClickListener);
        btnAbout.setOnClickListener(onItemClickListener);
        btnSetting.setOnClickListener(onItemClickListener);
    }

    @Override
    public void onResume() {
        super.onResume();
        showUserInfo();
    }


    //初始化状态改变
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onLoginEvent(LogoutEvent event) {
        showUserInfo();
    }

    private void showUserInfo() {
        String token = SystemContext.getInstance().getToken();
        if (token != null) {
            LoginInfo loginInfo = SystemContext.getInstance().getLoginInfo();
            String avatar = loginInfo != null ? loginInfo.getAvator() : null;
            if (avatar != null && !avatar.trim().equalsIgnoreCase("")) {
                avatar = avatar.replace(";", "");
                String imageUrl = ImageLoader.getInstance().getImageUrl(avatar);
                ImageLoader.getInstance().displayHeaderPic(MeFragment.this.getActivity(), imageUrl, imgvAvatar);
            } else {
                Glide.with(MeFragment.this.getActivity()).load(R.drawable.ic_message_avatar).into(imgvAvatar);
            }
            String nickname = SystemContext.getInstance().getUsername();
            txtvName.setText(nickname);
            imgvGender.setImageResource(R.drawable.ic_me_gender_male);
            imgvGender.setVisibility(View.VISIBLE);
        } else {
            imgvAvatar.setImageResource(R.drawable.ic_message_avatar);
            String nickname = SystemContext.getInstance().getUsername();
            txtvName.setText(nickname);
            imgvGender.setImageResource(R.drawable.ic_me_gender_male);
            imgvGender.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    //头像点击按钮
    private View.OnClickListener onAvatarClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String token = SystemContext.getInstance().getToken();
            if (token == null) {
                Intent intent = new Intent(MeFragment.this.getActivity(), LoginActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            } else {
                Toast.makeText(MeFragment.this.getActivity(), "用户已登录", Toast.LENGTH_SHORT).show();
            }
        }
    };

    //编辑按钮
    private View.OnClickListener onEditClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MeFragment.this.getActivity(), PersonInfoActivity.class);
            MeFragment.this.getActivity().startActivity(intent);
        }
    };

    //Button按钮
    private View.OnClickListener onButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view == btnWallet) {
                Intent intent = new Intent(MeFragment.this.getActivity(), WalletActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            } else if (view == btnInfo4Me) {
                Intent intent = new Intent(MeFragment.this.getActivity(), PersonInfoActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            } else if (view == btnSafe) {
                Intent intent = new Intent(MeFragment.this.getActivity(), SafeActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            } else {
                Intent intent = new Intent(MeFragment.this.getActivity(), ShareActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            }
        }
    };

    private View.OnClickListener onItemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view == btnHelp) {
                Intent intent = new Intent(MeFragment.this.getActivity(), HelpActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            } else if (view == btnFeedback) {
                Intent intent = new Intent(MeFragment.this.getActivity(), FeedbackActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            } else if (view == btnAbout) {
                Intent intent = new Intent(MeFragment.this.getActivity(), AboutActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            } else {
                Intent intent = new Intent(MeFragment.this.getActivity(), SettingActivity.class);
                MeFragment.this.getActivity().startActivity(intent);
            }
        }
    };


}
